package org.stvd.repository.workflow.leave.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.workflow.leave.BsLeaveInfo;
import org.stvd.repository.base.impl.BaseDaoImpl;
import org.stvd.repository.workflow.leave.BsLeaveInfoDao;
 
/**
 * @Title BsLeaveInfoDaoImpl
 * @Destribution:请假管理数据层实现类
 * @author fenglong
 * @date 2020年11月2日
 */
@Repository("BsLeaveInfoDao")
public class BsLeaveInfoDaoImpl extends BaseDaoImpl<BsLeaveInfo> implements BsLeaveInfoDao {
 
    @Override
    public QueryResult<Map<String, Object>> getQueryResult(int start, Integer pageSize, String depName, String empName,
            Date startDate, Date endDate, String userId, String status) {
        Map<String, Object> params = new HashMap<String, Object>();
        String sql = "SELECT CAST(bli.id AS CHAR) leaveId,bli.* FROM bs_leave_info bli WHERE 1=1 and bli.create_user =:userId";
        if(!StringUtil.isEmpty(depName)) {
            sql = sql + " AND (bli.dep_name like :depName)";
            params.put("depName", "%"+depName+"%");
        }
        if(!StringUtil.isEmpty(empName)) {
            sql = sql + " AND (bli.emp_name like :empName)";
            params.put("empName", "%"+empName+"%");
        }
        if(!StringUtil.isEmpty(status)) {
            sql = sql + " AND (bli.audit_status = :status)";
            params.put("status", status);
        }
        if (startDate != null) {
            sql += " AND bli.start_date >= :startDate";
            params.put("startDate", startDate);
        }
        if (endDate != null) {
            sql += " AND bli.end_date <= :endDate";
            params.put("endDate", endDate);
        }
        params.put("userId", userId);
        sql = sql + " order by bli.create_time desc";
        return getQueryResultBySQLToMap(start, pageSize, sql, params);
    }
 
    @Override
    public BsLeaveInfo findByProcessId(String processInstanceId) {
        List<BsLeaveInfo> list = findByHQL("FROM BsLeaveInfo WHERE processId = ?0", processInstanceId);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    @Override
    public QueryResult<Map<String, Object>> queryLeaveResult(int i, Integer pageSize, String procStatus, String uid,
            String role) {
        Map<String, Object> params = new HashMap<String, Object>();
        String sql = "SELECT CAST(boi.id AS CHAR) leaveInfoId,boi.*, wi.candidate, wi.group_id, wi.assignee, wi.task_id, wi.task_name, wi.status proc_status"
                + " FROM bs_leave_info boi, wf_index wi WHERE boi.process_id = wi.proc_inst_id"
                + " AND audit_uid like  '%" + uid + "%'";
        if ("ROLE_DEP_LEADER".equals(role)) {
            sql += "AND ( wi.assignee = :userId or boi.audit_status in('20','10')) AND boi.create_user != :userId ";
        } else if ("ROLE_MANAGE".equals(role)) {
            sql += "AND ( wi.assignee = :userId or boi.audit_status in('20'))";
        }else {
            sql += "AND ( wi.assignee = :userId or boi.audit_status in('20'))";
       }
        params.put("userId", uid);
 
        if (!StringUtil.isEmpty(procStatus)) {
            sql += " AND (boi.audit_status = :procStatus OR '' =:procStatus) ";
            params.put("procStatus", procStatus);
        }
        sql += " ORDER BY boi.create_time DESC";
        return getQueryResultBySQLToMap1(i, pageSize, sql, params);
    }
 
    @Override
    public List<BsLeaveInfo> NowisLeave(String userId, Date nowDate) {
        List<BsLeaveInfo> list = findByHQL(
            "FROM BsLeaveInfo WHERE createUser = ?0 AND startDate <= nowDate AND endDate >= nowDate AND auditStatus = '30'",
            userId, nowDate);
        return list;
    }
 
    @Override
    public List<BsLeaveInfo> findBySendUid(String currentUserid, String auditStatus) {
        List<BsLeaveInfo> list = findByHQL("FROM BsLeaveInfo WHERE (sendUid = ?0) and (auditStatus = ?1 or '' = ?1) ",
                currentUserid, auditStatus);
        return list;
    }
     public QueryResult<Map<String, Object>> getQueryResultBySQLToMap1(int start, int limit, String sql,
        Map<String, Object> params) {
        QueryResult<Map<String, Object>> queryResultMap = new QueryResult<Map<String, Object>>();
        //
        String countSQLHeard = "select count(*) ";
        if (sql.toLowerCase().indexOf("order by") > -1) {
          countSQLHeard = countSQLHeard + sql.substring(sql.toLowerCase().indexOf("from"), sql.toLowerCase().indexOf("order by"));
        } else {
          countSQLHeard = countSQLHeard + sql.substring(sql.toLowerCase().indexOf("from"));
        }
        long total = getCountBySQL(countSQLHeard, params);
        queryResultMap.setPageCount((int)Math.ceil((double)total * 1.0D / (double)limit));
        queryResultMap.setPageIndex((int)Math.floor((double)start * 1.0D / (double)limit) + 1);
        queryResultMap.setTotal(total);
        if (queryResultMap.getTotal() > 0L) {
            queryResultMap.setRows(this.findBySQLToMap((queryResultMap.getPageIndex() - 1) * limit, limit, sql, params));
        }
        return queryResultMap;
    }
}
